package com.alibaba.annotation;

import java.io.*;
import java.lang.reflect.Method;

/**
 * @Author: zhuangzhiwen
 * @Date: 2021/8/22
 */
public class ReflectTest3 {
    public static void main(String[] args) throws Exception {
        Class c1 = Class.forName("com.alibaba.annotation.Calculate");
        Calculate object = (Calculate) c1.newInstance();
        Method[] methods = c1.getMethods();

        int number=0;
        BufferedWriter bi=new BufferedWriter(new FileWriter("D:\\study\\spring_bean\\bug.txt"));

        for (Method method : methods) {
            if(method.isAnnotationPresent(Check.class)){
                try {
                    System.out.println(method.getName());
                    method.invoke(object);
                } catch (Exception e) {

                    number++;
                    bi.write(method.getName()+"方法异常");
                    bi.newLine();
                    bi.write("出现的异常信息是："+e.getCause().getMessage());
                    bi.newLine();
                    bi.write("出现的异常方法是："+e.getCause().getClass().getSimpleName());
                    bi.newLine();
                }
            }
        }
        bi.write("异常出现"+number+"次");
        bi.flush();
        bi.close();
    }
}
